import React, { Component } from 'react';
import { Switch, Route, Redirect } from "react-router-dom";
import dashboardRoutes from './routes/dashboard';
import Tool from "./utils/Tool";
import initStore from './state/storeBus';
import PropTypes from 'prop-types';
import './doc/assets/css/material-dashboard-react.css';
import './assets/stylus/base.styl';

const switchRoutes = (
  <Switch>
    {dashboardRoutes.map((prop, key) => {
      if (prop.redirect) {
        return <Redirect from={prop.path} to={prop.to} key={key} />;
      }
      return <Route path={prop.path} component={prop.component} key={key} />;
    })}
  </Switch>
);

class App extends Component {
    constructor () {
      super();
      // 初始化状态管理
      this.store = initStore();;
      Tool.getUrlParam('token') && window.sessionStorage.setItem('token', Tool.getUrlParam('token'));
      Tool.getUrlParam('name') && window.sessionStorage.setItem('name', Tool.getUrlParam('name'));
    }

    static childContextTypes = {
      store: PropTypes.object
    }

    getChildContext () {
      return {
        store: this.store
      };
    }

    render() {
        const routerDom =
                <div className="sc-oms-container">
                  {switchRoutes}
                </div>;
        return routerDom;
    }
}

export default App;
